<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	public function actionIndex()
	{
        $data = array(
            );

		$this->render('index.tpl',$data);
	}

	/**
	 * When occur an unknown error,redirect to an error page
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('site/error.tpl', array('error'=>$error));
	    }
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		if(!isset($_POST['username']) || trim($_POST['username'] === '')){
			$this->render('user/login.tpl',array('message'=>'请填写用户名'));
			return;
		}
		$model=new LoginForm;
		$model->username = trim($_POST['username']);
		$model->password = trim($_POST['password']);
		$model->rememberMe = true;//isset($_POST['rememberMe']) ? true : false;
		
		if($model->login()){           
			$this->redirect(Yii::app()->user->returnUrl);
		} else {
			$this->render('user/login.tpl',array('message'=>'用户名或密码错误'));
		}
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionReg()
	{
		$this->render('user/reg.tpl',array());
	}

	/**
	 * Displays the contact page.This method is not used yet.
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}
}
